package com.controller;

import com.bean.dto.ProductDto;
import com.bean.vo.ChainTypeQueryVo;
import com.bean.vo.ProductInfomationQueryVo;
import com.service.ChainTypeService;
import com.service.ProductInfomationService;
import com.service.ProductSpecificationService;
import com.util.ProductDtoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("pi")
public class ProductInfomationController {
    @Autowired
    ProductInfomationService pis;
    @Autowired
    ProductSpecificationService pss;
    @Autowired
    ChainTypeService cts;
    /*查询产品信息（全查，单查，模糊）*/
    @ResponseBody
    @RequestMapping("query")
    public ProductDto QueryAll(Integer page, Integer limit,ProductInfomationQueryVo piqv){
        System.out.println(">>>>>>>>带条件查询（产品信息）");
        System.out.println(piqv);
        System.out.println(page);
        System.out.println(limit);
        page= (page-1)*limit;
        piqv.setStart_code(page);
        piqv.setPage_size(limit);
        List<ProductInfomationQueryVo> list = pis.QueryAll(piqv);
        if (list.size()!=0){
            int count = pis.count(piqv);
            return ProductDtoUtil.returnSuccess(list,count);
        }
        return ProductDtoUtil.returnFail("无结果");
    }
    /*保存产品信息*/
    @ResponseBody
    @RequestMapping("save")
    public ProductDto add(ProductInfomationQueryVo piqv){
        System.out.println(">>>>>>>>>新增产品信息");
        System.out.println(piqv);
        Long i = pss.add(piqv);
        System.out.println("i："+i);
        piqv.getPi().setProduct_specificationId(i);
        int count = pis.add(piqv);
        System.out.println("count:"+count);
        if (count != 0){
            return ProductDtoUtil.returnSuccess("添加成功!");
        }
        return ProductDtoUtil.returnFail("添加失败");
    }
    /*产品信息添加或修改时速别编号下拉框内容获取*/
    @ResponseBody
    @RequestMapping("get")
    public ProductDto get(){
        System.out.println(">>>>>速别表编号获取");
        List<ChainTypeQueryVo> list = cts.Query();
        System.out.println(list.size());
        System.out.println(list);
        if (list.size()!= 0){
            return ProductDtoUtil.returnSuccess(list,0);
        }
        return ProductDtoUtil.returnFail("未知错误");
    }
    /*修改产品信息*/
    @ResponseBody
    @RequestMapping("update")
    public ProductDto update(ProductInfomationQueryVo piqv){
        System.out.println(">>>>>>>>>修改单体产品信息");
        System.out.println(piqv);
        Long id = pss.update(piqv);
        System.out.println("id："+id);
        piqv.getPi().setProduct_specificationId(id);
        System.out.println("修改时的对象："+piqv);
        int count = pis.upDate(piqv);
        System.out.println("count:"+count);
        if (count!=0){
            return ProductDtoUtil.returnSuccess("修改成功!");
        }
        return ProductDtoUtil.returnFail("修改失败");
    }
    /*删除产品信息（真删除）*/
    @ResponseBody
    @RequestMapping("delete")
    public ProductDto delete(ProductInfomationQueryVo piqv){
        System.out.println(">>>>>>>删除（真删除）");
        int count = pis.delete(piqv);
        if (count != 0){
            System.out.println("success!");
            return ProductDtoUtil.returnSuccess("删除成功!");
        }
        System.out.println("wrong!");
        return ProductDtoUtil.returnFail("删除失败！");
    }
    /*删除产品信息（假删除）*/
    @ResponseBody
    @RequestMapping("fakeDelete")
    public ProductDto fakeDelete(ProductInfomationQueryVo piqv){
        System.out.println(">>>>>>>>>>>>>删除（假）");
        System.out.println(piqv);
        int count = pis.fakeDelete(piqv);
        if (count != 0){
            System.out.println("success!");
            return ProductDtoUtil.returnSuccess("删除成功!");
        }
        System.out.println("wrong!");
        return ProductDtoUtil.returnFail("删除失败！");
    }
}
